<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Scripts\Tools;

use Srv\Libs\Boot;
use Srv\Libs\Common\CommString;
use Srv\Libs\Frame\Route;
use Srv\Libs\Storage\DataMysql;

require_once dirname(__DIR__, 2).'/Libs/Boot.php';
Boot::runConf('');

exit;
// 路由权限
$routeList                      = Route::initData();
$authKeyList                    = [];
$headList   = [
    ['name' => 'path',      'column' => 48, 'type' => 1],
    ['name' => 'class',     'column' => 32, 'type' => 1],
    ['name' => 'method',    'column' => 32, 'type' => 1],
    ['name' => 'auth',      'column' => 28, 'type' => 1],
    ['name' => 'DTO',       'column' => 32, 'type' => 1],
    ['name' => 'VO',        'column' => 32, 'type' => 1],
    ['name' => 'Comment',   'column' => 26, 'type' => 1],
];
foreach ($routeList[0] as $groupName => $pathList){
    ksort($pathList);
    $websPathDataList   = [];
    foreach ($pathList as $pathUrl => $pathData){
        $classExists    = strlen($pathData[0]) > 0 ? class_exists($pathData[0], true) ? 1 : 2 : 0;
        $dtoExists      = strlen($pathData[3]) > 0 ? class_exists($pathData[3], true) ? 1 : 2 : 0;
        $voExists       = strlen($pathData[4]) > 0 ? class_exists($pathData[4], true) ? 1 : 2 : 0;
        $websPathDataList[] = [
            'path'      => ltrim($pathUrl, '/'),
            'class'     => ($classExists === 2 ? '__' : '').substr($pathData[0], 14),
            'method'    => $pathData[1],
            'auth'      => $pathData[2],
            'code'      => $pathData[3],
            'DTO'       => ($dtoExists === 2 ? '__' : '').substr($pathData[4], 19),
            'VO'        => ($voExists === 2 ? '__' : '').substr($pathData[5], 18),
            'Comment'   => $pathData[6],
        ];
        if(strlen($pathData[2]) > 0) $authKeyList[$pathData[2]] = ['key' => $pathData[2], 'name' => $pathData[6]];
    }
    echo CommString::getTableShellStr($websPathDataList, $headList, 'webs;'.$groupName);
}
echo "\n\n";
foreach ($routeList[1] as $groupName => $pathList){
    ksort($pathList);
    $socksPathDataList  = [];
    foreach ($pathList as $pathUrl => $pathData){
        $classExists    = strlen($pathData[0]) > 0 ? class_exists($pathData[0], true) ? 1 : 2 : 0;
        $dtoExists      = strlen($pathData[3]) > 0 ? class_exists($pathData[3], true) ? 1 : 2 : 0;
        $voExists       = strlen($pathData[4]) > 0 ? class_exists($pathData[4], true) ? 1 : 2 : 0;
        $socksPathDataList[] = [
            'path'      => ltrim($pathUrl, '/'),
            'class'     => ($classExists === 2 ? '__' : '').substr($pathData[0], 15),
            'method'    => $pathData[1],
            'auth'      => $pathData[2],
            'code'      => $pathData[3],
            'DTO'       => ($dtoExists === 2 ? '__' : '').substr($pathData[4], 19),
            'VO'        => ($voExists === 2 ? '__' : '').substr($pathData[5], 18),
            'Comment'   => $pathData[6],
        ];
        if(strlen($pathData[2]) > 0) $authKeyList[$pathData[2]] = ['key' => $pathData[2], 'name' => $pathData[6]];
    }
    echo CommString::getTableShellStr($socksPathDataList, $headList, 'socks;'.$groupName);
}
echo "\n\n";
$keyExisList    = [];
$takrIdMax      = 0;
$DbAuto         = DataMysql::tag('auto', 'DB_SCRIPT');
$tokenKeyList   = $DbAuto->getData('SELECT takr_id, takr_key FROM '.$DbAuto->getTableNameAll('adm_auth_key_record'));
if(count($tokenKeyList) > 0) foreach ($tokenKeyList as $tokenKeyItem){
    $takrId         = intval($tokenKeyItem['takr_id']);
    if($takrId < 1) continue;
    $authKey        = trim($tokenKeyItem['takr_key']);
    $keyExisList[]  = $authKey;
    $takrIdMax      = max($takrIdMax, $takrId);
}
$sql            = 'INSERT INTO '.$DbAuto->getTableNameAll('adm_auth_key_record').' (takr_id, takr_key, takr_name, takr_lock, takr_level) VALUES ';
$sqlData        = '';
ksort($authKeyList);
foreach ($authKeyList as $authKeyItem){
    $authKey    = trim($authKeyItem['key']);
    $authName   = trim($authKeyItem['name']);
    if(strlen($authKey) < 1 || in_array(strtoupper($authKey), ['NULL', 'DENY', 'LOGIN'], true)) continue;
    if(in_array($authKey, $keyExisList, true)) continue;
    $sqlData    .= (strlen($sqlData) < 1 ? '' : ', ').'('.(++$takrIdMax).', \''.$DbAuto->getRealEscapeString($authKey).'\', \''.$DbAuto->getRealEscapeString($authName).'\', 0, 2)';
}
echo $sql.$sqlData."\n\n\n";
